package com.ruiyin.util;

import java.io.InputStream;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.jms.TextMessage;
import javax.servlet.http.HttpServletRequest;
import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

import java.io.InputStream;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

import com.thoughtworks.xstream.XStream;


public class MessageUtil
{
  public static Map<String, String> xmlToMap(HttpServletRequest req)
  {
    Map map = new HashMap();
    SAXReader resader = new SAXReader();
    try {
      InputStream ins = req.getInputStream();
      Document doc = resader.read(ins);
      Element root = doc.getRootElement();
      List<Element> list = root.elements();
      for (Element e : list) {
        map.put(e.getName(), e.getText());
      }
      ins.close();
    } catch (Exception e) {
      e.printStackTrace();
    }
    return map;
  }

  public static String textMessagetoXml(TextMessage textMessage)
  {
    XStream xstream = new XStream();
    xstream.alias("xml", textMessage.getClass());
    return xstream.toXML(textMessage);
  }
}